#include <iostream>
#include <cstring>
using namespace std;

struct myVoector
{
    int *pbuff;
    int len;
    myVoector():pbuff(NULL),len(0){
        cout << "无参构造函数" << endl;
    }
    myVoector(int *p,int len);//有参构造函数
    myVoector(const myVoector&);//拷贝构造
    ~myVoector(){
        delete[] pbuff;
        cout<<"析构函数"<< endl;
    }
    void travel();//遍历
    void push(int a);//后插
    void sort();//排序

};


int main(){



    return 0;
}

//有参构造函数
myVoector::myVoector(int *p,int len):pbuff(p),len(len)
{
    cout << "有参构造函数" << endl;
    if(len > 0 && p != NULL)
    {
        pbuff = new int[len];
        memcpy(pbuff , p , sizeof(int) * len);
    }
    else
    {
        pbuff = NULL;
    }
}

//拷贝构造函数
myVoector::myVoector(const myVoector&a)
{
    cout << "拷贝构造函数" << endl;
    this->len = a.len;
    if(a.len > 0 & a.pbuff != NULL)
    {
        pbuff = new int[len];
        memcpy(pbuff , a.pbuff , sizeof(int)*len);
    }
    else
    {
        pbuff = NULL;
    }
}



//遍历
void myVoector::travel()
{
    for(int i=0;i<len;i++)
    {
        cout << this->pbuff[i] << " ";
    }
    cout << endl;
}

//后插
void myVoector::push(int a)
{
    int *pnew = new int[len+1];
    if(len>0)
    {
        memcpy(pnew , pbuff , sizeof(int)*len);
    }
    pnew[len] = a;
    delete[] pbuff;
    pbuff = pnew;
    len++;
}

void myVoector:: sort() //排序
{
    for(int i=0;i<len-1;i++)
    {
        for(int j=0;j<len-i-1;j++)
        {
            if(pbuff[j] > pbuff[j+1])
            {
                swap(pbuff[j] , pbuff[j+1] );
            }
        }
    }
}
